package top.topsmart.api.bycenter.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.topsmart.api.bycenter.mapper.DynamicMapper;
import top.topsmart.api.bycenter.mapper.InfoMapper;
import top.topsmart.api.bycenter.mapper.UserMapper;
import top.topsmart.api.bycenter.mapper.UserOperationMapper;
import top.topsmart.api.bycenter.model.Dynamic;
import top.topsmart.api.bycenter.model.Info;
import top.topsmart.api.bycenter.model.User;
import top.topsmart.api.bycenter.entity.view.DynamicView;
import top.topsmart.api.bycenter.entity.pojo.PageEntity;
import top.topsmart.api.bycenter.model.UserOperation;
import top.topsmart.api.bycenter.service.DynamicService;
import top.topsmart.api.bycenter.util.Result;
import top.topsmart.api.bycenter.util.TimeHandler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class DynamicServiceImpl implements DynamicService {
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private InfoMapper infoMapper;
    @Autowired
    private UserOperationMapper userOperationMapper;

    @Override
    public Result release(JSONObject dynamic) {
        if (dynamic.getString("publisher") == null) {
            return new Result(1521, "publisher不能为空");
        } else {
            if (dynamic.getInteger("mood") == null) {
                return new Result(1521, "mood不能为空");
            } else {
                if (dynamic.getString("content") == null) {
                    return new Result(1521, "content不能为空");
                } else {
                    if (dynamic.getString("device") == null) {
                        return new Result(1521, "device不能为空");
                    } else {
                        String dynamicId = UUID.randomUUID().toString();

                        Dynamic newDynamic = new Dynamic();
                        newDynamic.setId(dynamicId);
                        newDynamic.setPublisher(dynamic.getString("publisher"));
                        newDynamic.setArea(dynamic.getString("area"));
                        newDynamic.setMood(dynamic.getInteger("mood"));
                        newDynamic.setContent(dynamic.getString("content"));
                        newDynamic.setImage(dynamic.getString("image"));
                        newDynamic.setVideo(dynamic.getString("video"));
                        newDynamic.setDevice(dynamic.getString("device"));
                        newDynamic.setReleaseTime(new Date());
                        newDynamic.setViews(0);
                        newDynamic.setDelFlag(false);
                        dynamicMapper.insertSelective(newDynamic);

                        JSONObject data = new JSONObject();
                        data.put("dynamicId", dynamicId);
                        return new Result(0, "success", data);
                    }
                }
            }
        }

    }

    @Override
    public Result update(JSONObject dynamic) {
        if (dynamic.getString("id") == null) {
            return new Result(1522, "id不能为空");
        } else {
            Dynamic newDynamic = new Dynamic();
            newDynamic.setId(dynamic.getString("id"));
            newDynamic.setArea(dynamic.getString("area")); //修改发布范围
            newDynamic.setMood(dynamic.getInteger("mood")); //修改心情
            newDynamic.setContent(dynamic.getString("content"));//修改内容
            newDynamic.setImage(dynamic.getString("image"));//修改图片
            newDynamic.setVideo(dynamic.getString("video")); //修改视频
            newDynamic.setDevice(dynamic.getString("device")); //修改发布设备
            newDynamic.setViews(dynamic.getInteger("views")); //修改发布设备
            newDynamic.setUpdateTime(new Date()); //修改时间
            newDynamic.setDelFlag(dynamic.getBoolean("delFlag"));//删除动态
            dynamicMapper.updateByPrimaryKeySelective(newDynamic);
            return new Result(0, "success");
        }
    }

    /**
     * page limit type
     *
     * @param param
     * @return
     */
    @Override
    public Result backByType(JSONObject param) {
        //参数异常处理

        Integer PAGE = param.getInteger("page");
        Integer LIMIT = param.getInteger("limit");
        String TYPE = param.getString("type");
        if (PAGE >= 1 && LIMIT >= 1 && TYPE != null) {
            if (TYPE.equals("all") || TYPE.equals("content") || TYPE.equals("image") || TYPE.equals("video")) {
                //创建结果接受容器
                JSONObject data = new JSONObject();
                List<DynamicView> dynamicViews = new ArrayList<DynamicView>();

                PageEntity pageEntity = JSONObject.parseObject(param.toJSONString(), PageEntity.class);
                pageEntity.setPage((PAGE - 1) * LIMIT);
                List<Dynamic> dynamics = dynamicMapper.backByType(pageEntity);
                if (dynamics.size() == 0) {
                    data.put("dynamicViews", dynamicViews);
                } else {
                    for (int i = 0; i < dynamics.size(); i++) {
                        DynamicView dynamicView = new DynamicView();

                        Dynamic dynamic = dynamics.get(i);
                        User user = userMapper.selectByPrimaryKey(dynamic.getPublisher());
                        Info info = infoMapper.selectByPrimaryKey(user.getInfoId());

                        UserOperation SParam_loves = new UserOperation();
                        SParam_loves.setObject(dynamic.getId());
                        Integer loves = userOperationMapper.getLoves(SParam_loves).size();
                        UserOperation SParam_commits = new UserOperation();
                        SParam_commits.setObject(dynamic.getId());
                        Integer commits = userOperationMapper.getCommits(SParam_commits).size();

                        dynamicView.setId(dynamic.getId());
                        dynamicView.setPublisher(dynamic.getPublisher());
                        dynamicView.setArea(dynamic.getArea());
                        dynamicView.setAvatar(info.getAvatar());
                        dynamicView.setNickname(info.getNickname());
                        dynamicView.setLevel(user.getLevel());
                        dynamicView.setMood(dynamic.getMood());
                        dynamicView.setContent(dynamic.getContent());
                        dynamicView.setImage(dynamic.getImage());
                        dynamicView.setVideo(dynamic.getVideo());
                        dynamicView.setDevice(dynamic.getDevice());
                        dynamicView.setReleaseTime(new TimeHandler().TimeHandler(dynamic.getReleaseTime()));
                        dynamicView.setViews(dynamic.getViews());
                        dynamicView.setLoves(loves);
                        dynamicView.setCommits(commits);
                        //追加进去数组
                        dynamicViews.add(dynamicView);
                    }
                    data.put("dynamicViews", dynamicViews);
                }
                return new Result(0, "success", data);
            }else {
                return new Result(1529,"type合法参数：type:'all','content','image','video'");
            }
        } else {
            return new Result(1529,"page,limit合法参数：page>=1 limit>=1");
        }


    }
}
